/*
 *验证:
 *属性包括:
 *		_dataType:{string,int,float,date}
 *		_cnName:
 *		_dataEmpty:{false,true}
 *		_dataLength:
 */
var g_fValidateForm = new Object();
g_fValidateForm.regexp_temp = null;
g_fValidateForm.regexp_ip = /^((25[0-5]|2[0-4]\d|[01]\d\d|\d?\d)\.){3}(25[0-5]|2[0-4]\d|[01]\d\d|\d?\d)$/;	//IPv4正则表达式{ xxx.xxx.xxx.xxx}(0.0.0.0 - 255.255.255.255)
g_fValidateForm.regexp_date = /^(([12]\d)?\d\d)[\\\/\.\s-,]((0?[1-9])|(1[0-2]))[\\\/\.\s-,]((0?[1-9])|([12]\d)|(3[01]))$/;	//日期正则表达式{ yyyy-mm-dd, yy-mm-dd, yyyy\mm\dd, yy\mm\dd, yyyy/mm/dd, yy/mm/dd, yyyy.mm.dd, yy.mm.dd, yyyy,mm,dd, yy,mm,dd, yyyy mm dd, yy mm dd}
g_fValidateForm.regexp_dateArray = /^(\d{1,4})[^\d]*(\d{1,2})[^\d]*(\d{1,2})$/;	//将日期分割成数组的正则表达式
g_fValidateForm.regexp_int = /^[+-]?[0-9]*$/;	//整数正则表达式
g_fValidateForm.regexp_float = /^[+-]?[0-9]*\.?[0-9]*$/;	//实数正则表达式
g_fValidateForm.regexp_time1 = /^(\d|[01]\d|2[0-3]):(\d|[0-5]\d)$/;	//时间正则表达式{ hh:mm}(00:00 - 23:59)

function ValidateForm(oForm) {
	var elements = oForm.elements;
	for (var i=0;i<elements.length;i++) {
		var element = elements(i);
		var elementCnName = "";
		var elementDataEmpty = "";
		var elementDataType = "";
		var elementDataLength = 0;	//字符串长度,1汉字为2个字符长度
		var elementCDataLength = 0;	//字符串长度,1汉字为1字符长度
		
		if (element._dataType != null) {
			elementDataType = element._dataType.toLowerCase();
		}
		
		if (element._cnName != null) {
			elementCnName = element._cnName.toLowerCase();
		}
		
		if (element._dataEmpty != null) {
			elementDataEmpty = element._dataEmpty.toLowerCase();
		}
		
		if (element._dataLength != null) {
			elementDataLength = element._dataLength.toLowerCase();
		}
		
		if (element._cDataLength != null) {
			elementCDataLength = element._cDataLength.toLowerCase();
		}
		//验证_dataEmpty
		if (elementDataEmpty == "false") {
			var s_value = "";
			if (element.tagName.toLowerCase() == "input" || element.tagName.toLowerCase() == "textarea" || element.tagName.toLowerCase() == "select") {
				s_value = element.value;
				if (StrLength(s_value) == 0) {
					alert(elementCnName+"不能为空值!");
					if (element.type.toLowerCase() != "hidden")
						element.focus();
					return false;
				}
			}
		}
		//验证_dataType
		if (elementDataType != "") {
			var s_value = "";
			switch (elementDataType) 
			{
				case "string":
					break;
				case "int":
					if(!g_fValidateForm.regexp_int.test(element.value))
					{
						try
						{
							//选择、定位
							element.select();
							element.focus();
						}
						catch(e)
						{
						}
						alert(elementCnName+"数值类型格式非法");
						return false;
					}
				case "float":
					if(!g_fValidateForm.regexp_float.test(element.value))
					{
						try
						{
							//选择、定位
							element.select();
							element.focus();
						}
						catch(e)
						{
						}
						alert(elementCnName+"实数类型格式非法");
						return false;
					}
			}
		}
		
		//验证DataLength
		if (elementDataLength != 0) {
			var s_value = "";
			switch (elementDataType) 
			{
				case "string":
					if (StrLength(element.value) > elementDataLength)
					{
						try
						{
							//选择、定位
							element.select();
							element.focus();
						}
						catch(e)
						{
						}
						alert(elementCnName+"字符长度超出限制,最大可为"+elementDataLength);
						return false;
					}
				case "int":
					break;
			}
		}
		
		//验证CDataLength
		if (elementCDataLength != 0) {
			var s_value = "";
			switch (elementDataType) 
			{
				case "string":
					if (element.value.length > elementCDataLength)
					{
						try
						{
							//选择、定位
							element.select();
							element.focus();
						}
						catch(e)
						{
						}
						alert(elementCnName+"字符长度超出限制,最大可为"+elementCDataLength);
						return false;
					}
				case "int":
					break;
			}
		}
	}
	alertOut();
	return true;
}

//获取字符串的长度
function StrLength(str) {
	var bytesCount = 0;
	for (var i = 0; i < str.length; i++)
	{
		var c = str.charAt(i);
		if (/^[\u0000-\u00ff]$/.test(c))
			bytesCount += 1;
		else
			bytesCount += 2;
	}
	return bytesCount;
}

//弹出等待界面
function alertOut(){
   //var h = window.document.body.scrollHeight;
   var h = window.screen.height;
   if(h<document.body.clientHeight){
    h=document.body.clientHeight;
   }
   var div_type = document.getElementById("alertshow");
   var div_hei = document.getElementById("divbackblack");
  
   div_type.style.left = (document.body.offsetWidth-300)/2;
   div_type.style.top = document.body.scrollTop+220;
  
   var s = document.getElementsByTagName("select");
   for( var i=0;i<s.length;i++){
    s[i].style.display="none";
   }  
    div_hei.style.height=h;
    div_hei.style.display = "block";
    div_type.style.display = "block";
}